package com.m3839.sdk.pay.ui.controller;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;

import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.util.ToastUtil;
import com.m3839.sdk.pay.bean.PayConfig;
import com.m3839.sdk.pay.bean.PayInfo;
import com.m3839.sdk.pay.bean.PayLimit;
import com.m3839.sdk.pay.bean.PayResult;
import com.m3839.sdk.pay.constant.PayConstant;
import com.m3839.sdk.pay.task.PayTask;
import com.m3839.sdk.pay.ui.PayDialogFragment;
import com.m3839.sdk.pay.ui.PayLimitDialogFragment;
import com.m3839.sdk.pay.ui.PayResultDialogFragment;
import com.m3839.sdk.pay.ui.PayRetryDialogFragment;

/**
 * @author ChenBaoyang
 * @description: PayUiControllerImpl
 * @date 2021/12/8 20:10
 */
public class PayUiControllerImpl implements IPayUiController{


    @Override
    public void showPayDialog(Activity activity, PayTask payTask, PayInfo payInfo, PayConfig config) {
        //找不到对应的支付渠道信息， 不显示弹窗
        if (!checkPayChannels(config)) {
            ToastUtil.showToast(activity, "充值维护中，给您带来不便，敬请谅解");
            return;
        }

        PayDialogFragment payDialogFragment =  PayDialogFragment.getInstance(payInfo, config);
        payDialogFragment.setPayTask(payTask);
        payDialogFragment.show(activity.getFragmentManager(), Constant.HYKB_TAG_FRAGMENT_PAY_SUBMIT);
    }

    private boolean checkPayChannels(PayConfig config) {
        boolean hasChannels = false;
        if (config != null && config.payChannels != null) {
            for (int i = 0; i < config.payChannels.size(); i++) {
                String channelValue = config.payChannels.get(i);
                if (TextUtils.equals(channelValue, PayConstant.HYKB_VALUE_PAY_CHANNEL_WECHAT_H5) ||
                        TextUtils.equals(channelValue, PayConstant.HYKB_VALUE_PAY_CHANNEL_ALIPAY)) {
                    hasChannels = true;
                }
                break;
            }
        }
        return hasChannels;
    }

    @Override
    public void showPayResultDialog(Activity activity, PayTask payTask, PayConfig config, PayResult result) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (activity == null || activity.isFinishing() || activity.isDestroyed()) {
                return;
            }
        } else {
            if (activity == null || activity.isFinishing()) {
                return;
            }
        }

        PayResultDialogFragment resultDialogFragment = (PayResultDialogFragment) activity.getFragmentManager()
                .findFragmentByTag(Constant.HYKB_TAG_FRAGMENT_PAY_RESULT);
        resultDialogFragment.setPayTask(payTask);
        if (resultDialogFragment != null) {
            resultDialogFragment.update(config, result);
        } else {
            resultDialogFragment = PayResultDialogFragment.getInstance(config, result);
            resultDialogFragment.show(activity.getFragmentManager(),
                    Constant.HYKB_TAG_FRAGMENT_ANTI);
        }
    }

    @Override
    public void showPayLimitDialog(Activity activity, PayTask payTask, PayLimit payLimit, PayInfo payInfo) {
        PayLimitDialogFragment dialogFragment = PayLimitDialogFragment.newInstance(payLimit, payInfo);
        dialogFragment.setPayTask(payTask);
        dialogFragment.show(activity.getFragmentManager(), Constant.HYKB_TAG_FRAGMENT_PAY_LIMIT);
    }

    @Override
    public void showRetryQueryPayOrder(Activity activity, PayTask payTask, PayResult payResult) {
        PayRetryDialogFragment dialogFragment = PayRetryDialogFragment.newInstance(payResult);
        dialogFragment.setPayTask(payTask);
        dialogFragment.show(activity.getFragmentManager(), Constant.HYKB_TAG_FRAGMENT_PAY_RETRY);
    }
}
